# -*- coding: utf-8 -*-
'''
@author: olivia.dou
Created on: 2022/12/20 10:29
desc: 微信登录页面 v8.0.27
'''
from appium.webdriver.common.appiumby import AppiumBy
from base.po.mobile_base_page import MobileBasePage
from base.po.po_util import screenshot_by_element


class WechatLoginPage(MobileBasePage):
    _login_btn_locator = (AppiumBy.XPATH, "//*[@resource-id='com.tencent.mm:id/j_9']")
    _switch_account_login_locator = (AppiumBy.XPATH, "//*[@resource-id='com.tencent.mm:id/g5x']")
    _account_input_locator = (AppiumBy.XPATH, "//*[@resource-id='com.tencent.mm:id/cd7' and @text='请填写微信号/QQ号/邮箱']")
    _pwd_input_locator = (AppiumBy.XPATH, "//*[@resource-id='com.tencent.mm:id/cd7' and @text='请填写密码']")
    _agree_and_login_locator = (AppiumBy.XPATH, "//*[@resource-id='com.tencent.mm:id/g5o']")
    _agree_privacy_locator = (AppiumBy.XPATH, "//*[@resource-id='weuiAgree']")
    _slider_locator = (AppiumBy.XPATH, "//*[@resource-id='tcImgArea']/following-sibling::*[1]")

    #滑动拼图验证页
    _orig_image_locator = (AppiumBy.XPATH, "//*[@resource-id='slideBgWrap']")
    _img_with_blank_locator = (AppiumBy.XPATH, "//*[@resource-id='slideBg']")



    def login(self):
        # try:
        #     # 打开可能是初始登录页，也可能是微信号登录页
        #     self.find_element_and_click(self._login_btn_locator)
        #     self.find_element_and_click(self._switch_account_login_locator)
        # except:
        #     pass

        self.find_element_and_input(self._account_input_locator, "13301766782")
        self.find_element_and_input(self._pwd_input_locator, "chinascope@1234")
        self.find_element_and_click(self._agree_and_login_locator)
        try:
            self.find_element_and_click(self._agree_privacy_locator,timeout=3) # 同意隐私保护指引, 页面source太大，获取会timeout
            self.find_element_and_click(self._agree_and_login_locator)
        except:
            pass


        orig_img_element = self.find_element(self._orig_image_locator)
        orig_img = screenshot_by_element(self.driver, orig_img_element)
        img_with_blank_element = self.driver.find_element(self._img_with_blank_locator)
        img_with_blank = screenshot_by_element(self.driver, img_with_blank_element)
        slider = self.driver.find_element(self._slider_locator)
        self.slide_jigsaw_verification(orig_img, img_with_blank, slider)





